//
//  SecondViewController2.m
//  真·豆瓣半完全体
//
//  Created by dllo on 15/12/31.
//  Copyright © 2015年 lanou3g. All rights reserved.
//

#import "SecondViewController2.h"
#import "SecondCollectionViewCell.h"
#import "UIImageView+WebCache.h"

#define W  self.view.frame.size.width
#define H  self.view.frame.size.height
@interface SecondViewController2 ()<UICollectionViewDataSource, UICollectionViewDelegate, UIScrollViewDelegate>

@property (nonatomic, retain) UICollectionView *collectionView;
@property (nonatomic, retain) UIScrollView *scrollView;


@end

@implementation SecondViewController2
- (void)dealloc{

    [super dealloc];
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self    =  [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"电影";
    
    UIImage *img = [UIImage imageNamed:@"btn_nav_list@2x.png"];
    img = [img imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithImage:img style:UIBarButtonItemStylePlain target:self action:@selector(itemAction:)];
    self.navigationItem.rightBarButtonItem = item;
    
    
    [self creatColletionView];
    [self creatScrollView];

}


/** 创建轮播图 */
- (void)creatScrollView{
    self.scrollView =[[ UIScrollView alloc] initWithFrame:CGRectMake(0, 0, W, 200)];
    _scrollView.contentSize = CGSizeMake(W * 28 , 0);
    _scrollView.delegate = self;
    _scrollView.bounces = NO;
    _scrollView.pagingEnabled = YES;
    _scrollView.showsHorizontalScrollIndicator = NO;
    _scrollView.showsVerticalScrollIndicator = NO;
    _scrollView.contentOffset = CGPointMake(W, 0);    //初始偏移量
    [self.view addSubview:_scrollView];
    [_scrollView release];
    
    //给轮播图添加图片
    for (int i = 0; i < _arrData2.count; i++) {
        UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(W * (i +1), 0, W, 200)];
        [_scrollView addSubview:imgView];
        NSMutableDictionary *dic = _arrData2[i][@"images"];
        [imgView sd_setImageWithURL:[NSURL URLWithString:dic[@"large"]]];
        [imgView release];
    }
    
    UIImageView *lastImgView = [[UIImageView alloc] initWithFrame:CGRectMake(W * 27 , 0, W, 200)];
    [_scrollView addSubview:lastImgView];
    NSMutableDictionary *dic = [_arrData2 firstObject][@"images"];
    [lastImgView sd_setImageWithURL:[NSURL URLWithString:dic[@"large"]]];
    [lastImgView release];
    
    UIImageView *firstImgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, W, 200)];
    [_scrollView addSubview:firstImgView];
    NSMutableDictionary *dic1 = [_arrData2 lastObject][@"images"];
    [firstImgView sd_setImageWithURL:[NSURL URLWithString:dic1[@"large"]]];
    [firstImgView release];
    
    [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(timeAction:) userInfo:nil repeats:YES];

}
/** 定时方法 */
- (void) timeAction:(NSTimer * )timer{
    [_scrollView setContentOffset:CGPointMake(_scrollView.contentOffset.x + W, 0) animated:YES];
    if (_scrollView.contentOffset.x == W * 27) {
        _scrollView.contentOffset = CGPointMake(W, 0);
    }
    else if (_scrollView.contentOffset.x == 0){
        _scrollView.contentOffset = CGPointMake(W * 26, 0);
    }
}
#pragma mark - ScrollViewDelegate
/** 轮播图偏移 回弹结束时 */
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    if (_scrollView.contentOffset.x == W * 27) {
        _scrollView.contentOffset = CGPointMake(W, 0);
    }
    else if (_scrollView.contentOffset.x == 0){
        _scrollView.contentOffset = CGPointMake(W * 26, 0);
    }
}


/** 创建collectionView */
- (void) creatColletionView{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    layout.itemSize = CGSizeMake((self.view.frame.size.width - 40) / 3, 150);
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    
    
    self.collectionView  = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 210, 375, self.view.frame.size.height - 323) collectionViewLayout:layout];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_collectionView];
    [_collectionView release];
    
    [_collectionView registerClass:[SecondCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
}

#pragma mark - CollectionView代理方法
/** cell个数 */
- (NSInteger) collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return _arrData2.count;
}
/** 创建CollectionViewCell */
- (UICollectionViewCell *) collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
     SecondCollectionViewCell *cell =[collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    
    
    NSMutableDictionary *dic = _arrData2[indexPath.row][@"images"];
    [cell.imgView sd_setImageWithURL:[NSURL URLWithString:dic[@"large"]]];
    
    cell.label.text = _arrData2[indexPath.row][@"title"];
    
    return cell;
}
/** 点击方法 */
- (void) collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"%ld", indexPath.row);
}




- (void) itemAction:(UIBarButtonItem *)item{
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
